namespace SimpleWebServices.Web.Mvc
{
    using System.Globalization;
    using System.Text;
    using System.Web.Mvc;
    using System.Web.Routing;
    using SimpleWebServices.Globalization;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;

    [InternalServerError]
    [Allow(Methods = "GET, HEAD, OPTIONS")]
    [CacheControl, ContentLanguage, ContentLocation, ContentMD5]
    [Resource("site")]
    public sealed class SiteController : ResourceController, IRegisterRoutes
    {
        public void Register(RouteCollection routes)
        {
            routes.Register(typeof(SiteController), true);
        }

        [ContentNegotiation(".html", "*/*, text/*, text/html")]
        public ActionResult HtmlRepresentation(CultureInfo language)
        {
            language.SetCurrentCulture();

            Site model = Site.Information;
            this.ViewData.Model = model;

            return new ContentResult
            {
                ContentEncoding = Encoding.UTF8,
                ContentType = "text/html",
                Content = SiteController.TextHtml(model)
            };
        }

        [ContentNegotiation(".json", "text/json, application/json, application/vnd.simplewebservices-site+json")]
        public ActionResult JsonRepresentation(CultureInfo language)
        {
            language.SetCurrentCulture();

            Site model = Site.Information;
            this.ViewData.Model = model;

            return new JsonSerializationResult("application/vnd.simplewebservices-site+json", model);
        }

        [ContentNegotiation(".txt", "text/plain")]
        public ActionResult TextRepresentation(CultureInfo language)
        {
            language.SetCurrentCulture();

            Site model = Site.Information;
            this.ViewData.Model = model;

            return new TextResult(model);
        }

        [ContentNegotiation(".xhtml", "application/xhtml+xml")]
        public ActionResult XhtmlRepresentation(CultureInfo language)
        {
            language.SetCurrentCulture();

            Site model = Site.Information;
            this.ViewData.Model = model;

            return new ContentResult
            {
                ContentEncoding = Encoding.UTF8,
                ContentType = "application/xhtml+xml",
                Content = SiteController.ApplicationXhtml(model)
            };
        }

        [ContentNegotiation(".xml", "text/xml, application/xml, application/vnd.simplewebservices-site+xml")]
        public ActionResult XmlRepresentation(CultureInfo language)
        {
            language.SetCurrentCulture();

            Site model = Site.Information;
            this.ViewData.Model = model;

            return new XmlSerializationResult("application/vnd.simplewebservices-site+xml", model);
        }

        private static string HtmlBody(Site model)
        {
            StringBuilder assemblies = new StringBuilder();
            foreach (string assembly in model.Assemblies)
            {
                assemblies.AppendFormat(CultureInfo.InvariantCulture, "<li>{0}</li>", assembly);
            }

            return
                "<body>" +
                    "<h1>" + model.Title + "</h1>" +
                    "<h2>Server</h2>" +
                    "<dl id='server'>" +
                        "<dt>Host</dt>" +
                        "<dd id='host'>" + model.Host + "</dd>" +
                        "<dt>Machine</dt>" +
                        "<dd id='machine'>" + model.Machine + "</dd>" +
                    "</dl>" +
                    "<h2>Assemblies</h2>" +
                    "<ul id='assemblies'>" +
                        assemblies.ToString() +
                    "</ul>" +
                "</body>";
        }

        private static string ApplicationXhtml(Site model)
        {
            return
                "<?xml version='1.0' encoding='utf-8' ?>" +
                "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" +
                "<html xmlns='http://www.w3.org/1999/xhtml'>" +
                    "<head>" +
                        "<title>" + model.Title + "</title>" +
                        "<link rel='canonical' href='http://" + model.Host + "/site.en.html' />" +
                        "<meta content='application/xhtml+xml' http-equiv='Content-Type' />" +
                        "<meta content='application/xhtml+xml' scheme='IMT' name='DC.format' />" +
                        "<meta content='Text' scheme='DCMIType' name='DC.type' />" +
                        "<meta content='NOFOLLOW,NOINDEX' name='robots' />" +
                    "</head>" +
                    SiteController.HtmlBody(model) +
                "</html>";
        }

        private static string TextHtml(Site model)
        {
            return
                "<html>" +
                    "<head>" +
                        "<title>" + model.Title + "</title>" +
                        "<link rel='canonical' href='http://" + model.Host + "/site.en.html'>" +
                        "<meta content='text/html' http-equiv='Content-Type'>" +
                        "<meta content='text/html' scheme='IMT' name='DC.format'>" +
                        "<meta content='Text' scheme='DCMIType' name='DC.type'>" +
                        "<meta content='NOFOLLOW,NOINDEX' name='robots'>" +
                    "</head>" +
                    SiteController.HtmlBody(model) +
                "</html>";
        }
    }
}